package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Intent;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.ZuiPaiSheActivity;
import com.mmlc.meimei.lichengkongjian.dialog.CalendarDialog;
import com.mmlc.meimei.lichengkongjian.dialog.ResponseListener;
import com.mmlc.meimei.lichengkongjian.home.WebViewActivity;
import com.mmlc.network.filter.ZuiPaiSheRequestFilter;
import com.mmlc.network.responsebean.ZuiPaisheResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.XUtils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2018/1/15.
 */

public class ZuiMeiPaiSheFragment extends InterFragment {

    PullToRefreshGridView pullToRefreshGridView;
    List<ZuiPaisheResponseBean.DataBean.ListBean> datas=new ArrayList<>();
    CommonAdapter<ZuiPaisheResponseBean.DataBean.ListBean> adapter;
    int page = 0;
    @Override
    protected int initResourceID() {
        return R.layout.fragment_zuimeipaishe;
    }

    String start_time="",end_time="";
    TextView sTime,eTime;
    @Override
    protected void initView() {
        pullToRefreshGridView= (PullToRefreshGridView) view.findViewById(R.id.pull_refresh_list);
        final Date date=new Date();
//        start_time=DateUtils.changeDateStyle(date,"yyyy-MM-dd");
//        end_time=DateUtils.addDay(date,"yyyy-MM-dd",10);
        sTime= (TextView) view.findViewById(R.id.start_time);
        eTime= (TextView) view.findViewById(R.id.end_time);
//        sTime.setText(start_time);
//        eTime.setText(end_time);
        sTime.setOnClickListener(this);
        eTime.setOnClickListener(this);
        view.findViewById(R.id.confirm).setOnClickListener(this);
        view.findViewById(R.id.yuyue).setOnClickListener(this);
        pullToRefreshGridView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshGridView.setAdapter(adapter=new CommonAdapter<ZuiPaisheResponseBean.DataBean.ListBean>(getActivity(),R.layout.content_zuimeipaishe,datas) {
            @Override
            protected void convert(ViewHolder viewHolder,final ZuiPaisheResponseBean.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.text,item.getAdd_day());
                XUtils.image((ImageView) viewHolder.getView(R.id.image),item.getImg_icon());
                viewHolder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent in=new Intent(getActivity(), ZuiPaiSheActivity.class);
                        in.putExtra("title",item.getAdd_day());
                        startActivity(in);
                    }
                });
            }
        });
        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) throws InterruptedException {
                page=1;
                datas.clear();
                pullToRefreshGridView.setMode(PullToRefreshBase.Mode.BOTH);
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                page++;
                getData();
            }
        });
        getData();
    }
    boolean isFirst=true;
    String url="";
    private void getData() {
        ZuiPaiSheRequestFilter z=new ZuiPaiSheRequestFilter((BaseParentActivity) getActivity());
        z.requestBean.stime=start_time;
        z.requestBean.etime=end_time;
        z.requestBean.pageNow=page+"";
        z.requestBean.pageSize=""+20;
        if(isFirst){
            z.requestBean.stime="";
            z.requestBean.etime="";
            isFirst=false;
        }
        z.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ZuiPaisheResponseBean>() {

            @Override
            public void onSuccess(ZuiPaisheResponseBean result) {
                if (result.getCode() == 0) {
                    url=result.getData().getBeauty_h5();
                    datas.addAll(result.getData().getList());
                    adapter.notifyDataSetChanged();
                    pullToRefreshGridView.onRefreshComplete();
                    if(result.getData().getList().size()==0){
                        pullToRefreshGridView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    }
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.confirm:
                datas.clear();
                getData();
                break;
            case R.id.yuyue:
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("title", "预约拍摄")
                        .putExtra("url", url);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getActivity().startActivity(intent);
                break;
            case R.id.start_time:
                choiseDay(1);
                break;
            case R.id.end_time:
                choiseDay(2);
                break;
        }
    }
    void choiseDay(final int flag){
        CalendarDialog c=new CalendarDialog();
        c.setInitDateAndListner("",flag==1?"开始时间":"结束时间", new ResponseListener<String>() {
            @Override
            public void response(String data) {
                if(flag==1){
                    sTime.setText(data);
                    start_time=data;
                }else{
                   eTime.setText(data);
                    end_time=data;
                }
            }
        });
        c.show(getChildFragmentManager(),"");
    }
}
